3 Rules of Database Design:


1. First Normal Form- (no columns of data or duplicate rows of data)
2. Second Normal Form- (each attribute is dependant on the entire primary key)
3. Third Normal Form- (each attribute is ONLY dependent on the primary key)

Definitions:

Primary Key: An attribute that is guaranteed to be unique for each record. (PK)

Foreign Key: A Field in a relational table that matches the primary key column for another table. The foreign key can also be used to cross reference tables. (FK)

First Normal Form:

First normal form requires that each attribute of the relation is atomic. Each column must contain only a single value and each row must contain the same columns.

In this example, the First Normal Form is broken because one customer may have more than one phone number therefore, the same information will be entered more than once creating a duplicate row of data.


Customer ID (PK)
First Name
Last Name
Email Address
1
Darren
Choi
dchoimail



Dchoi
2
Greg
Sra
gregsra
The Solution, create two related tables, each with an unique primary key.



Customer ID (PK)
First Name
Last Name
1
Darren
Choi
2
Greg
Sra

Email ID (PK)
Customer ID (FK)
Email Address
1
1
dchoimail
2
1
dchoi
3
2
gregsra






Another Example of 1NF
Song ID
Track name
Track Length
Genre
1
Empire State of Mind
276
Rap
2
Like a G6
216
Rock



Pop

Song ID
Track Name
Track Length
1
Empire State of Mind
276
2
Like a G6
216

Genre ID
Song ID
Genre
1
1
Rap
2
2
Rock
3
2
Pop

Second Normal Form:
Second normal form requires that data in all non-key fields be dependent on the primary key. The objective of second normal form is to avoid data redundancy in your database.

In this example, the Second normal Form is broken because not every attribute is completely dependent on the primary key. We would not know the store address if we did not know the store ID, therefore, the Store address breaks this rule because it does not depend on the Store Category attribute.


Store Category (PK)
Store ID
Owner
Store Address
Food and Beverage
1
Greg Sra
123 Real Street
Retail
2
Darren Choi
456 Real Street
The solution, we split up the two keys to make each attribute dependent on it's primary key.

Store ID (PK)
Store Category (PK)
Owner
1
Food and Beverage
Greg Sra
2
Retail
Darren Choi


Category ID (PK)
Store Address (FK)
Store ID
A
123 Real Street
1
B
45 Real Street
2

Third normal form:

The table has to already be formatted in first normal and second normal form. Each attribute must only be dependant on the
primary key, the primary key can also be the foreign key.

In this example, Third Normal Form is broken because there are many attributes that do not relate to the primary key.

Customer ID (PK)
First Name
Last Name
Address
Amount of Reward Received
Store ID
Payment
1
Greg
Sra
1234 Fake Street
3
2
Credit Card
2
Darren
Choi
5678 Fake Street
5
5
Cash
The Solution, we split up the table into sections, and put the information into the tables that they correspond to.


Customer ID (PK)
First Name
Last Name
Address
1
Greg
Sra
1234 Fake Street
2
Darren
Choi
5678 Fake Street


Customer ID (PK)
Store ID
Amount of Rewards Received
1
2
3
2
5
5


Store ID (PK)
Payment
2
Credit Card
5
Cash

Entity Relationship (ER) Diagrams

Below is an example of a entity relationship diagram, it shows the possible database for the assets for a business.

external image image007.png
[1]

Rules of Entity Relationship Diagrams:

  1. A Primary key can never have a blank value.
  2. A non primary key can have a blank value.


One to One Relationship example: One student has one locker number
One to Many Relationship example: One student has many classes
Many to Many Relationship example: One teacher can have many students and many students can have one teacher.
  1. ^ Links used in this page. http://www.codeproject.com/KB/database/SQLInterviewQuestions/image007.png